// 路由文件
import {
	createRouter,
	createWebHistory
} from "vue-router";
import config from "../config"
import Query from '../views/query/workbench.vue'
//判断是否已加载过动态/静态路由
var isGetRouter = false;
const routes = [{
		path: '/',
		name: 'souye',
		component: Query
	},
	{
		path: '/new',
		name: 'souye1',
	component: () => import( /* webpackChunkName: "layout" */ '@/views/query/editor.vue'),
	},
	{
		path: '/home',
		name: 'Home',
		component: () => import( /* webpackChunkName: "layout" */ '@/views/Home.vue'),
	}, {
		path: '/test',
		name: 'test',
		component: "queryLayout",
		children: [{
			path: '/uu',
			name: 'testUU',
			component: () => import( /* webpackChunkName: "layout" */ '@/views/Home.vue'),
		}]


	}
]

const router = createRouter({
	history: createWebHistory(),
	routes
})
// 遍历后台传来的路由字符串，转换为组件对象
const filterAsyncRouter = function (asyncRouterMap) {
	return asyncRouterMap.filter(route => {
		if (route.component) {
			// Layout组件特殊处理
			if (route.component === 'queryLayout') {
				route.component = Query
			}
		}
		if (route.children != null && route.children && route.children.length) {
			route.children = filterAsyncRouter(route.children)
		}
		return true
	})
}

router.beforeEach((to, from, next) => {
	//加载动态/静态路由
	if (!isGetRouter) {
		let menu = [...routes, ...config.STATIC_ROUTERS]
		filterAsyncRouter(menu)
		menu.forEach(item => {
			router.addRoute(item)
		})
		isGetRouter = true;
		next({
			...to,
			replace: true
		}) // hack方法 确保addRoutes已完成
	} else {
		next();
	}
})

export default router;
